﻿using Newtonsoft.Json;
using RTW.OA.Areas.Common.Servers;
using RTW.OA.Controllers;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Common.Controllers
{
    public class AttendController : BaseController
    {
        private PaibanServers paibanServers;
        private EvectionServers evectionServers;
        private LeaveServers leaveServers;
        private OutServers outServers;
        private OverTimeServers overtimeServers;

        public AttendController()
        {
            paibanServers = new PaibanServers();
            evectionServers = new EvectionServers();
            leaveServers = new LeaveServers();
            outServers = new OutServers();
            overtimeServers = new OverTimeServers();
        }

        // GET: Common/Evection
        public ActionResult Paiban()
        {
            return View();
        }

        // GET: Common/Evection
        public ActionResult PaibanCalendar()
        {
            return View();
        }

        // GET: Common/Paiban/Edit/5
        public ActionResult PaibanEdit(int? id)
        {
            ViewBag.LEADER_IDData = paibanServers.GetLEADER_ID();
            var model = new attend_paiban();
            if (id != null)
            {
                ViewBag.Title = "编辑排班表";
                model = GetPaibanModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建排班表";
            }
            return View(model);
        }

        // POST: Common/Paiban/Edit/5
        [HttpPost]
        public ActionResult PaibanEdit(int? id, attend_paiban model)
        {
            ViewBag.LEADER_IDData = paibanServers.GetLEADER_ID();
            try
            {
                if (id == null || id == 0)
                {
                    paibanServers.Add(model);
                    ViewBag.msg = "新增排班表信息成功！";
                    model = new attend_paiban();

                }
                else
                {
                    paibanServers.Update(model);
                    ViewBag.msg = "修改排班表信息成功！";

                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Common/Paiban/Edit/5
        public ActionResult PaibanDetails(int? id)
        {
            ViewBag.LEADER_IDData = paibanServers.GetLEADER_ID();
            var model = new attend_paiban();
            if (id != null)
            {
                ViewBag.Title = "编辑排班表";
                model = GetPaibanModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建排班表";
            }
            return View(model);
        }

        // GET: Common/Paiban/Delete/5
        public ActionResult PaibanDelete(int id)
        {
            if (id != 0)
            {
                paibanServers.Del(id.ToString());
                ViewBag.msg = "删除排班表信息成功！";
            }
            return Content("");
        }

        // GET: Common/Evection
        public ActionResult Evection()
        {
            return View();
        }

        // GET: Common/Evection/Edit/5
        public ActionResult EvectionEdit(int? id)
        {
            ViewBag.LEADER_IDData = evectionServers.GetLEADER_ID();
            var model = new attend_evection();
            if (id != null)
            {
                ViewBag.Title = "编辑出差表";
                model = GetEvectionModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建出差表";
            }
            return View(model);
        }

        // POST: Common/Evection/Edit/5
        [HttpPost]
        public ActionResult EvectionEdit(int? id, attend_evection model)
        {
            ViewBag.LEADER_IDData = evectionServers.GetLEADER_ID();
            try
            {
                if (id == null || id == 0)
                {
                    evectionServers.Add(model);
                    ViewBag.msg = "新增出差表信息成功！";
                    model = new attend_evection();

                }
                else
                {
                    evectionServers.Update(model);
                    ViewBag.msg = "修改出差表信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Common/Evection/Edit/5
        public ActionResult EvectionDetails(int? id)
        {
            ViewBag.LEADER_IDData = evectionServers.GetLEADER_ID();
            var model = new attend_evection();
            if (id != null)
            {
                ViewBag.Title = "编辑出差表";
                model = GetEvectionModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建出差表";
            }
            return View(model);
        }

        // GET: Common/Evection/Delete/5
        public ActionResult EvectionDelete(int id)
        {
            if (id != 0)
            {
                evectionServers.Del(id.ToString());
                ViewBag.msg = "删除出差表信息成功！";
            }
            return Content("");
        }


        // GET: Common/Leave
        public ActionResult Leave()
        {
            return View();
        }

        // GET: Common/Leave/Edit/5
        public ActionResult LeaveEdit(int? id)
        {
            //ViewBag.LEAVE_TYPE2Data = leaveServers.GetLEAVE_TYPE2();
            var model = new attend_leave();
            if (id != null)
            {
                ViewBag.Title = "编辑请假表";
                model = GetLeaveModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建请假表";
            }
            return View(model);
        }

        // POST: Common/Leave/Edit/5
        [HttpPost]
        public ActionResult LeaveEdit(int? id, attend_leave model)
        {
            //ViewBag.LEAVE_TYPE2Data = leaveServers.GetLEAVE_TYPE2();
            try
            {
                if (id == null || id == 0)
                {
                    leaveServers.Add(model);
                    ViewBag.msg = "新增请假表信息成功！";
                    model = new attend_leave();

                }
                else
                {
                    model.ALLOW = "0";
                    model.STATUS = "1";
                    leaveServers.Update(model);
                    ViewBag.msg = "修改请假表信息成功！";

                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }


        // GET: Common/Leave/Approval/5
        public ActionResult LeaveApproval(int? id) {
            var model = GetLeaveModel(id.Value.ToString());
            return View(model);
        }
        
        public JsonResult SetApproval(int id, int status, string reason = null)
        {
            try
            {
                leaveServers.UpdateStatus(id, status, reason);
                return Json(new
                {
                    code = 100,
                    msg = "更新成功"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new
                {
                    code = 0,
                    msg = "更新失败，原因：" + e.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        // GET: Common/Leave/Edit/5
        public ActionResult LeaveDetails(int? id)
        {
            //ViewBag.LEAVE_TYPE2Data = leaveServers.GetLEAVE_TYPE2();
            var model = new attend_leave();
            if (id != null)
            {
                ViewBag.Title = "请假表明细";
                model = GetLeaveModel(id.Value.ToString());
            }
            return View(model);
        }

        // GET: Common/Leave/Delete/5
        public ActionResult LeaveDelete(int id)
        {
            if (id != 0)
            {
                leaveServers.Del(id.ToString());
                ViewBag.msg = "删除请假表信息成功！";
            }
            return Content("");
        }

        // GET: Common/Out
        public ActionResult Out()
        {

            return View();
        }

        // GET: Common/Out/Edit/5
        public ActionResult OutEdit(int? id)
        {
            var model = new attend_out();
            if (id != null)
            {
                ViewBag.Title = "编辑外出";
                model = GetOutModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建外出";
            }
            return View(model);
        }

        // POST: Common/Out/Edit/5
        [HttpPost]
        public ActionResult OutEdit(int? id, attend_out model)
        {
            try
            {
                if (id == null || id == 0)
                {
                    outServers.Add(model);
                    ViewBag.msg = "新增外出表信息成功！";
                    model = new attend_out();

                }
                else
                {
                    outServers.Update(model);
                    ViewBag.msg = "修改外出表信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Common/Out/Edit/5
        public ActionResult OutDetails(int? id)
        {
            var model = new attend_out();
            if (id != null)
            {
                model = GetOutModel(id.Value.ToString());
            }
            return View(model);
        }

        // GET: Common/Out/Delete/5
        public ActionResult OutDelete(int id)
        {
            if (id != 0)
            {
                outServers.Del(id.ToString());
                ViewBag.msg = "删除外出表信息成功！";
            }
            return Content("");
        }

        // GET: Common/OverTime
        public ActionResult OverTime()
        {

            return View();
        }

        // GET: Common/OverTime/Edit/5
        public ActionResult OverTimeEdit(int? id)
        {
            var model = new attend_overtime();
            if (id != null)
            {
                ViewBag.Title = "编辑加班表";
                model = GetOverTimeModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建加班表";
            }
            return View(model);
        }

        // POST: Common/OverTime/Edit/5
        [HttpPost]
        public ActionResult OverTimeEdit(int? id, attend_overtime model)
        {
            try
            {
                if (id == null || id == 0)
                {
                    overtimeServers.Add(model);
                    ViewBag.msg = "新增加班表信息成功！";
                    model = new attend_overtime();

                }
                else
                {
                    overtimeServers.Update(model);
                    ViewBag.msg = "修改加班表信息成功！";

                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Common/OverTime/Edit/5
        public ActionResult OverTimeDetails(int? id)
        {
            var model = new attend_overtime();
            ViewBag.Title = "加班表详情";
            model = GetOverTimeModel(id.Value.ToString());
            return View(model);
        }

        // GET: Common/OverTime/Delete/5
        public ActionResult OverTimeDelete(int id)
        {
            if (id != 0)
            {
                overtimeServers.Del(id.ToString());
                ViewBag.msg = "删除加班表信息成功！";
            }
            return Content("");
        }

        #region //GetData
        public string GetEvectionTableJsonResult()
        {
            var list = evectionServers.GetData();
            return JsonConvert.SerializeObject(list);
        }
        
        public attend_evection GetEvectionModel(string id)
        {
            var model = evectionServers.GetModelById(id);
            return model;
        }

        public string GetPaibanTableJsonResult()
        {
            var list = paibanServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        [HttpGet]
        public ActionResult GetPaibanCalendarResult()
        {
            var model = paibanServers.GetData();
            var list = new List<object>();
            foreach (var item in model)
            {
                list.Add(new
                {   
                    title = "时间：" + item.ZBSJ_B.Value.ToString("yyyy-MM-dd HH:mm:ss") + "\n值班人："+item.ZHIBANREN+"\n备注："+item.BEIZHU,
                    start = item.ZBSJ_B.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                    end = item.ZBSJ_E.HasValue ? item.ZBSJ_E.Value.ToString("yyyy-MM-dd HH:mm:ss"):null,
                    allDay = !item.ZBSJ_E.HasValue
                });
            }
            return Json(list,JsonRequestBehavior.AllowGet);
        }

        public attend_paiban GetPaibanModel(string id)
        {
            var model = paibanServers.GetModelById(id);
            return model;
        }

        public string GetLeaveTableJsonResult()
        {
            var list = leaveServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public attend_leave GetLeaveModel(string id)
        {
            var model = leaveServers.GetModelById(id);
            return model;
        }

        public string GetOutTableJsonResult()
        {
            var list = outServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public attend_out GetOutModel(string id)
        {
            var model = outServers.GetModelById(id);
            return model;
        }

        public string GetOverTimeTableJsonResult()
        {
            var list = overtimeServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public attend_overtime GetOverTimeModel(string id)
        {
            var model = overtimeServers.GetModelById(id);
            return model;
        }
        #endregion //GetData
    }
}
